import {
  find,
  cancel
} from '../../../api/askfor';
import {
  getInfo as mchGetInfo
} from '../../../api/enterprise'
const util = require('../../../utils/util.js')
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconLineOne:'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/dingdan/refund_lineOne.png',
    iconLineTwo:'https://rzico.oss-cn-shenzhen.aliyuncs.com/dinnerBar/dingdan/refund_lineTwo.png',
    refundForm:[
      {title:'退款申请成功，等待餐厅处理',date:'2020-12-04 12:56'},
      {title:'商家同意退款，等待到账',date:'2020-12-04 13:10'},
      {title:'退款成功',date:'2020-12-04 13:20'},
    ],
    id: '',
    detailsForm: null,
    orderLog: [],
    mch: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.data.id = options.id
    this.getInfoClick()
  },

  onShow:function(){
    this.findClick()
  },
  findClick() {
    var _this = this
    find(this.data.id).then(
      res => {
        if (res.type == 'success') {
          console.log(res)
          _this.data.detailsForm = res.data.askfor
          _this.data.detailsForm.createDate = util.datetimeFormat(_this.data.detailsForm.createDate)
          _this.data.orderLog = res.data.askforLog
          if (!util.isNull(_this.data.orderLog)) {
            _this.data.orderLog.forEach(function(item) {
              item.createDate = util.datetimeFormat(item.createDate)
            })
          }
          _this.data.detailsForm.itemList.forEach(function(item) {
            item.thumbnail = util.thumbnail(item.thumbnail, 120, 120)
          })
          _this.setData({
            detailsForm: _this.data.detailsForm,
            orderLog: _this.data.orderLog
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
  cancelClick() {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '是否确认取消',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
            mask:true
          })
          cancel(_this.data.id).then(
            res => {
              wx.hideLoading()
              if (res.type == 'success') {
                console.log(res)
                wx.showToast({
                  title: res.content,
                })
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          ).catch(error => {
            wx.hideLoading()
            console.log(error)
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  phoneClick(){
    var _this = this
    if (_this.data.mch.phone) {
      wx.makePhoneCall({
        phoneNumber: _this.data.mch.phone // 仅为示例，并非真实的电话号码
      })
    }else{
      wx.showToast({
        title: '暂无客服电话',
      })
    }
  },
  getInfoClick() {
    let _this = this
    console.info("我被执行了")
    mchGetInfo({ "mchId": app.globalData.mchId }).then(
      res => {
        console.log(res)
        if (res.type == 'success') {
          _this.data.mch = res.data
          _this.setData({
            mch: _this.data.mch
          })
        }
      }
    ).catch(error => {
      console.log(error)
    })
  },
})
